package event.model;

import java.sql.ResultSet;
import java.sql.SQLException;

public class User {
	private Dsek ds;
	int user_id;
	private String login;
	private String cardId;
	private String firstName;
	private String nickName;
	private String lastName;
	private String allergies;
	
	public User(String cardId){
		this.cardId = cardId;
		this.ds = new Dsek();
		this.user_id = ds.getIdFromCardId(this.cardId);
		try {
			setAttributes(ds.getUserInfo(user_id));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public User(int user_id) {
		this.user_id = user_id;
		this.ds = new Dsek();
		try { 
			setAttributes(ds.getUserInfo(user_id));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setAttributes(ResultSet rs) throws SQLException {
		rs.next();
		login = rs.getString("login");
		firstName = rs.getString("firstname");
		nickName = rs.getString("nickname");
		lastName = rs.getString("lastname");
		allergies = rs.getString("allergies");
	}
	
	public int getId(){
		return user_id;
	}

	public String getCardId(){
		return cardId;
	}
	
	public String getLogin(){
		return login;
	}
	
	public String getFirstNane(){
		return firstName;
	}
	
	public String getNickName(){
		return nickName;
	}
	
	public String getLastName(){
		return lastName;
	}
	
	public String getAllergies(){
		return allergies;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(firstName).append(" ");
		if (nickName != null && nickName != "")
			sb.append("\"").append(nickName).append("\" ");
		sb.append(lastName);
		
		return sb.toString();
	}
}